﻿using Gdmec.Cache;
using Gdmec.Load;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XLua;

namespace Gdmec.UI
{
    [LuaCallCSharp]
    public class UIBase 
    {
        public GameObject go;
        public LuaFunction onInit;
        public LuaFunction onUnit;
        public List<string> paths = new List<string>();
        public string name = string.Empty;

        public void AddPath(string path) 
        {
            paths.Add(path);
        }

        public void Open() 
        {
            LoadManager.Load(name, paths.ToArray(), () => 
            {
                go = CacheManager.CloneGO(string.Format("{0}/{1}", paths[0], name));
                go.transform.SetParent(UIManager.UIRoot, false);
                if (onInit != null) onInit.Call();
            });
        }

        public void Close() 
        {
            Object.Destroy(go);
            if (onUnit != null) onUnit.Call();
            go = null;
        }

    }
}
